package com.lyhengtongwl.zqsnews.myinterface;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.webkit.JavascriptInterface;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.base.Constant;
import com.lyhengtongwl.zqsnews.net.UpClickCount;
import com.lyhengtongwl.zqsnews.net.Url;
import com.lyhengtongwl.zqsnews.ui.activity.GoodsDetailActivity;
import com.lyhengtongwl.zqsnews.ui.activity.LoginAndRegisterActivity;
import com.lyhengtongwl.zqsnews.ui.activity.MainActivity;
import com.lyhengtongwl.zqsnews.ui.activity.NewsMyArticleActivity;
import com.lyhengtongwl.zqsnews.ui.activity.NewsWebGLActivity;
import com.lyhengtongwl.zqsnews.ui.activity.QCInvitationActivity;
import com.lyhengtongwl.zqsnews.ui.activity.RechargeActivity;
import com.lyhengtongwl.zqsnews.ui.activity.WebActivity;
import com.lyhengtongwl.zqsnews.utils.AppConfig;
import com.lyhengtongwl.zqsnews.utils.CopyUtils;
import com.lyhengtongwl.zqsnews.utils.NewsOpenShare;
import com.lyhengtongwl.zqsnews.utils.NewsToastUtils;
import com.lyhengtongwl.zqsnews.utils.SPUtils;
import com.lyhengtongwl.zqsnews.utils.ShowShareUtils;
import com.lyhengtongwl.zqsnews.utils.ToastUtil;
import com.lyhengtongwl.zqsnews.utils.UserUtils;

/**
 * Created by Steven on 2019/2/26.
 */
public class JsInterface {
    private FragmentActivity activity;

    /**
     * Instantiate the interface and set the context
     */
    public JsInterface(FragmentActivity activity) {
        this.activity = activity;
    }

    @JavascriptInterface
    public void toURL(String actionUrl) {

        activity.startActivity(new Intent(activity, WebActivity.class).putExtra("url", actionUrl + ""));
    }

    @JavascriptInterface
    public void toLogin() {
        if (!UserUtils.checkLogin())
            activity.startActivity(new Intent(activity, LoginAndRegisterActivity.class));
    }

    @JavascriptInterface
    public void toAccount() {
        activity.startActivity(new Intent(activity, MainActivity.class).putExtra("tab", 3));
        activity.finish();
    }

    @JavascriptInterface
    public void toShare(String actionUrl) {
        JSONObject obj = (JSONObject) JSON.parse(actionUrl + "");
        ShowShareUtils.showShareDialog(activity, activity.getSupportFragmentManager(), obj);
    }

    @JavascriptInterface
    public void toGoods(String actionUrl) {
        activity.startActivity(new Intent(activity, GoodsDetailActivity.class).putExtra("issuedNo", actionUrl + ""));
    }

    @JavascriptInterface
    public void toCategory(String actionUrl) {
        activity.startActivity(new Intent(activity, MainActivity.class).putExtra("tab", 1).putExtra("actionUrl", actionUrl));
        activity.finish();
    }

    @JavascriptInterface
    public void toRecharge(String actionUrl) {
        if (UserUtils.checkLogin()) {
            activity.startActivity(new Intent(activity, RechargeActivity.class).putExtra("money", actionUrl + ""));
        } else {
            activity.startActivity(new Intent(activity, LoginAndRegisterActivity.class));
        }
    }

    @JavascriptInterface
    public void toIndex() {
        activity.startActivity(new Intent(activity, MainActivity.class).putExtra("tab", 0));
        activity.finish();
    }

    @JavascriptInterface
    public void toMyShare() {
        ToastUtil.showShort(App.getContext(), "邀请好友");
    }

    /**
     * 新闻列表 网页
     */
    @JavascriptInterface
    public void toBack() {
        activity.finish();
    }

    @JavascriptInterface
    public void toForward() {
//        Log.e("toforward", "toForward: "+ );
        activity.startActivity(new Intent(activity, MainActivity.class).putExtra("tab", 0));
        activity.finish();
    }

    @JavascriptInterface
    public void toShareFriends() {
        new NewsOpenShare(activity).ShareUrlToWx(Constant.ShareDetail, AppConfig.getInstance(App.getContext()).get("redBagUrl") + "?shareCode=" + SPUtils.get(App.getContext(), "shareCode", ""), R.mipmap.red_packet, AppConfig.getInstance(App.getContext()).get("redBagUrl"), 0);
////        if (PermissionUtils.getInstance().checkPermission(activity,
////                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//        new Thread(new Runnable() {
//            @SuppressLint("CheckResult")
//            @Override
//            public void run() {
//
//                if (App.hbList != null && App.hbList.size() > 0)
//                    NewsShareUtils.shareImgToFriends(activity, SaveHttpImgUtils.returnBitMap(activity, App.hbList.get(0)));
////                        new NewsOpenShare(activity).shareImgToWXF(OpenSdkUtils.returnBitMap(activity, App.hbList.get(0)));
//
//            }
//        }).start();
////        } else {
////            PermissionUtils.getInstance().showSystemPermissionsSettingDialog(activity);
////        }
    }

    private void getPhonePermissiom() {


    }

    @JavascriptInterface
    public void toShareCircleFriends() {
        new NewsOpenShare(activity).ShareUrlToWx(Constant.ShareDetail, AppConfig.getInstance(App.getContext()).get("redBagUrl") + "?shareCode=" + SPUtils.get(App.getContext(), "shareCode", ""), R.mipmap.red_packet, AppConfig.getInstance(App.getContext()).get("redBagUrl"), 1);
//        if (PermissionUtils.getInstance().checkPermission(activity,
//                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//        new Thread(new Runnable() {
//            @SuppressLint("CheckResult")
//            @Override
//            public void run() {
//
//                if (App.hbList != null && App.hbList.size() > 0)
////                        new NewsOpenShare(activity).shareImgToWXFC(OpenSdkUtils.returnBitMap(activity, App.hbList.get(2)));
//
//
//                    NewsShareUtils.shareImgToCircleFriends(activity, SaveHttpImgUtils.returnBitMap(activity, App.hbList.get(2)));
//            }
//
//        }).start();
////        } else {
////            PermissionUtils.getInstance().showSystemPermissionsSettingDialog(activity);
////        }
    }

    @JavascriptInterface
    public void toQCInvite() {
        activity.startActivity(new Intent(activity, QCInvitationActivity.class));
    }

    @JavascriptInterface
    public String getToken() {
        return SPUtils.get(App.getContext(), Constant.CommonInfo.TOKEN, "") + "";
    }

    @JavascriptInterface
    public void copyUrl() {
        CopyUtils.copyText(App.getContext(), AppConfig.getInstance(App.getContext()).get("downLoadUrl"));
        NewsToastUtils.showToast(App.getContext(), "复制成功~");
    }

    @JavascriptInterface
    public void copyWx() {
        CopyUtils.copyText(App.getContext(), AppConfig.getInstance(App.getContext()).get("wxNumber"));
        NewsToastUtils.showToast(App.getContext(), "复制成功~");
    }

    @JavascriptInterface
    public void toZQGL() {
        UpClickCount.getInstance(activity).upClick("strategy", "攻略");
        activity.startActivity(new Intent(activity, NewsWebGLActivity.class).putExtra("url", AppConfig.getInstance(App.getContext()).get("strategyUrl")).putExtra("isDefault", "true"));
    }

    @JavascriptInterface
    public void copyService() {
        CopyUtils.copyText(App.getContext(), AppConfig.getInstance(App.getContext()).get("wechat"));
        NewsToastUtils.showToast(App.getContext(), "复制成功~");
    }

    @JavascriptInterface
    public void toUploadList() {
        activity.startActivity(new Intent(activity, NewsMyArticleActivity.class).putExtra("type", "1"));
    }

    @JavascriptInterface
    public void toFullScreenWebView() {
        activity.startActivity(new Intent(activity, NewsWebGLActivity.class).putExtra("url", AppConfig.getInstance(App.getContext()).get("WXLINETIP")).putExtra("isDefault", "true"));
    }
}
